<script>
import axios from "axios";

export default {
  name: "Article",
  data() {
    return {
      markdownOption: {},
      tableData: [],
      title: "",
      content: null,
      currentPage: 1,
      pageSize: 5,
      total: 0,
      loading: true,
      multipleSelection: [],
      form: {},
      formLabelWidth: '100px',
      dialogFormVisible1: false,
      dialogFormVisible: false,
      dialogViewVisible: false,
      id: null,
      ids: [],
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
    }
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      this.request.get("/article/selectAll", {
        params: {
          title: this.title,
          currentPage: this.currentPage,
          pageSize: this.pageSize,
        }
      }).then(res => {
        this.tableData = res.data.records;
        this.loading = false;
        this.total = res.data.total;

      });
    },
    imgAdd(pos, $file) {
      let $vm = this.$refs.md;
      let formData = new FormData();
      formData.append('file', $file);
      axios({
        url:  this.baseUrl+'/file/upload',
        method: 'post',
        data: formData,
        headers: {'Content-Type': 'multipart/form-data'}
      }).then((res) => {
        console.log(res);
        $vm.$img2Url(pos, res.data.data);
      })
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.load();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.load();
    },
    reset() {
      this.title = "";
      this.currentPage = 1;
      this.pageSize = 5;
      this.total = 0;
      this.load();
    },
    handleDelete(id) {
      this.request.delete("/article/delete/" + id).then(res => {
        if (res.code == 200) {
          this.$message({
            message: '删除成功',
            type: 'success',
            center: true
          });
          this.load();
        } else {
          this.$message({
            showClose: true,
            message: res.msg,
            type: 'error'
          });
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val.map((a) => a.id);
      console.log(this.multipleSelection);
    },
    handleBatchRemove() {
      let ids = this.multipleSelection;
      this.request.post("/article/deleteBatch", ids).then(res => {
        if (res.code == 200) {
          this.$message({
            message: '删除成功',
            type: 'success',
            center: true
          });
          this.load();
        } else {
          this.$message({
            center: true,
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
    handleEdit(row) {
      this.form = row;
      this.id = row.id;
      this.dialogFormVisible1 = true;
    },
    edit() {
      this.request.post("/article/edit/" + this.id, this.form).then(res => {
        if (res.code === 200) {
          this.$message({
            message: '文章修改成功',
            type: 'success',
            center: true
          });
          this.dialogFormVisible1 = false;
          this.form = {};
          this.load();
        } else {
          this.$message({
            message: res.msg,
            type: 'error',
            center: true
          });
        }
      })
    },
    save() {
      this.request.post("/article/save", this.form).then(res => {
        if (res.code === 200) {
          this.$message({
            message: '文章创建成功',
            type: 'success',
            center: true
          });
          this.dialogFormVisible = false;
          this.form = {};
          this.load();
        } else {
          this.$message({
            message: res.msg,
            type: 'error',
            center: true
          });
        }
      })
    },
    view(content) {
      console.log(content);
      this.content = content;
      this.dialogViewVisible = true;
    },
    headerBg({row, rowIndex}) {
      if (rowIndex == 0) {
        return 'headerBgClass'
      }
    }
  }
}
</script>

<template>
  <div style="color:#666666">
    <div style="margin:10px 0px">
      <el-input style="width:300px" size="small" v-model="title" suffix-icon="el-icon-search"
                placeholder="请输入名称"></el-input>
      <el-button type="primary" class="ml-5" @click="load" size="small">搜索</el-button>
      <el-button type="warning" class="ml-5" @click="reset" size="small">重置</el-button>
    </div>
    <div style="margin:10px 0px">
      <div style="padding: 10px 0;border-bottom: 1px dashed #ccc" v-for="item in tableData" :key="item.id">
        <div class="pd-10" style="font-size: 20px;color: #409EFF;cursor:pointer " @click="$router.push('/front/articleDetail?id='+item.id)" >{{ item.title }}</div>
        <div style="font-size:14px;margin-top: 10px ">
          <i class="el-icon-user-solid" style="margin-right: 5px"></i><span>{{ item.publisher }}</span>
          <i class="el-icon-time" style="margin-left:20px;margin-right: 5px"></i><span>{{ item.pubdate }}</span>
        </div>
      </div>
    </div>

    <div style="padding:10px 0px">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

  </div>

</template>

<style>


</style>

